﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Data;
using System.Text;


namespace Actuals.Data
{
    public class DalProject : Dal<IProject, Project>, IDalProject
    {
           private readonly IDalManager _manager;
        private static readonly IDalProject _instance = new DalProject();
        private const string SP_GET_PROJECT = "usp_GetProject";
        private const string SP_GET_PROJECTS = "usp_GetProjects";
        private const string SP_ADD_UPDATE_PROJECT = "usp_AddUpdateProject";
        
        private const string SP_DELETE_PROJECT = "usp_DeleteProject";

        public static IDalProject Instance
        {
            get { return _instance; }
        }

        public DalProject()
        {
            _manager = new DalManager();
        }

        public DalProject(IDalManager manager)
        {
            _manager = manager;
        }

        protected override void Convert(IProject obj, DataRow row)
        {
            obj.ProjectId = _manager.GetValue<int>(row, "ProjectId");
            obj.ProjectDescription = _manager.GetValue<string>(row, "ProjectDescription");
            obj.StartDate = _manager.GetValue<DateTime?>(row, "StartDate");
            obj.EndDate = _manager.GetValue<DateTime?>(row, "EndDate");
            obj.TeamId = _manager.GetValue<int>(row, "TeamID");
        }

        #region IDalProject Members

        public IProject GetProject(int projectId)
        {
            _manager.StoredProcedureName = SP_GET_PROJECT;
            _manager.AddInParameter("@ProjectId", DbType.Int32, projectId);
            

           IProject project = Convert(_manager.ExecuteDataSet());

            return project;
        }

        public List<IProject> GetProjects()
        {
            _manager.StoredProcedureName = SP_GET_PROJECTS;

            List<IProject> projects = new List<IProject>();
            foreach (IProject project in ConvertAll(_manager.ExecuteDataSet()))
                projects.Add(project);

            return projects;

           
        }

        #endregion


        public bool AddProject(IProject project)
        {
            _manager.StoredProcedureName = SP_ADD_UPDATE_PROJECT;
            _manager.AddInParameter("@ProjectId", DbType.Int32, project.ProjectId);
            _manager.AddInParameter("@ProjectDescription", DbType.String, project.ProjectDescription);
            _manager.AddInParameter("@TeamId", DbType.Int32, project.TeamId);
            _manager.ExecuteNonQuery();
            return true;
        }

        public bool UpdateProject(IProject project)
        {
            return AddProject(project);
        }

        public bool DeleteProject(int projectId)
        {
            _manager.StoredProcedureName = SP_DELETE_PROJECT;
            _manager.AddInParameter("@ProjectId", DbType.Int32, projectId);
         
            _manager.ExecuteNonQuery();
            return true;
        }
    }
}
